<?php
namespace app\api\controller\kuyou\applet;
use think\Db;
class GoodsSalesSummaryc extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $where = [];$map = [];
            $code = $this->request->post('purchase_code');
            if(!empty($code)){
                $where['i.purchase_code'] = ['like','%'.$code.'%'];
            }
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['i.purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $map = [];
            $customers_id = $this->request->post('customers_id');
            if(!empty($customers_id)){
                $map['sp.id'] = $customers_id;
            }
            $sale_user = $this->request->post('sale_user');
            if(!empty($sale_user)){
                $where['i.sale_user'] = $sale_user;
            }
            $goods_code = $this->request->post('goods_code');
            if(!empty($goods_code)){
                $where['i.goods_code'] = $goods_code;
            }
            $keyword = $this->request->post('keyword');
            if(!empty($keyword)){
                $where['i.purchase_code|i.goods_code'] = $keyword;
            }

            $field = empty(input('field'))?'customers_id':trim(input('field'));
            $order = empty(input('sort'))?'DESC':trim(input('sort'));

            $sup = Db::name('kuyou_customers')->alias('sp')
                ->join('kuyou_customertype gt','sp.customertype_id=gt.id')
                ->where($map)
                ->field('sp.id,sp.customer_name,gt.type_name')
                ->select();

            //获取设置
            $set= \think\Db::name('kuyou_sets')->field('type,name,value,is_checked,set')->select();
            $setList = [];
            foreach ($set as $set_k=>$set_v){
                if(!empty($set_v['set'])){
                    $set_v['set'] = unserialize($set_v['set']);
                }
                $setList[$set_v['name']] = $set_v;
            }
            //数量价格格式化
            $number_decimal = $setList['number_decimal']['value'];
            $price_decimal  = $setList['price_decimal']['value'];

            $listarr = [];
            foreach ($sup as $sup_k=>$sup_v){
                $list = [];
                $lists = Db::name('kuyou_goods_sales_summary')->alias('i')
                    ->join('kuyou_goods g','g.goods_code=i.goods_code')
                    ->join('kuyou_stores s','s.id=i.stores_id')
                    ->join('kuyou_unit u','u.id=i.goods_unit')
                    ->where($where)
                    ->where(['i.customers_id'=>$sup_v['id']])
                    ->order(['i.'.$field=>$order])
                    ->group('i.goods_code,i.stores_id')
                    ->field('sum(i.tax_money) as tax_money,sum(i.money_tax_price) as money_tax_price,i.goods_code,i.stores_id,sum(basic_nmber) as basic_nmber,sum(number) as i_number,unit_price,sum(all_money) as all_money,i.default_unit,i.goods_unit,i.purchase_money,
                g.goods_code,g.goods_name,g.goods_specification_id,g.goods_barcode,g.unit_id,g.more_unit,
                s.name as s_name,u.name as u_name')
                    ->select();

                if(!empty($lists)){
                    foreach ($lists as $lists_k=>$lists_v){
                        $lists_v['i_number'] = $lists_v['basic_nmber'];

                        $unit_conversion = '';
                        $unit = Db::name('kuyou_unit')->where(['id'=>$lists_v['unit_id']])->field('id,name')->find();
                        if($unit){
                            $lists_v['u_name'] = $unit['name'];
                            $unit_name = $unit['name'];
                            $rowadvance_rate = '1';
                            if($lists_v['more_unit']==1){
                                $more_unit = Db::name('kuyou_unit_conversion')->alias('uc')
                                    ->join('kuyou_unit u','u.id=uc.vice_unit')
                                    ->where(['uc.main_unit'=>$unit['id']])
                                    ->order('uc.advance_rate','ASC')
                                    ->field('uc.vice_unit,u.name,u.id,uc.advance_rate')
                                    ->select();
                                foreach ($more_unit as $unit_k=>$unit_v){
                                    $unit_name .= ','.$unit_v['name'];
                                    $rowadvance_rate .= ':'.$unit_v['advance_rate'];
                                    if($unit_v['id']==$lists_v['goods_unit']){
                                        $lists_v['advance_rate'] = $unit_v['advance_rate'];
                                        $lists_v['more_unit_name'] = $unit_v['name'];
                                    }
                                }
                                $unit_conversion = $unit_name.'('.$rowadvance_rate.')';
                            }
                        }
                        $lists_v['unit_conversion'] = $unit_conversion;

                        if(!empty($list)){
                            foreach ($list as $list_k=>$list_v){
                                if($list_v['goods_code']==$lists_v['goods_code'] && $list_v['stores_id']==$lists_v['stores_id']){
                                    $list[$list_k]['i_number'] += $lists_v['i_number'];
                                    $list[$list_k]['all_money'] += $lists_v['all_money'];
                                    $list[$list_k]['purchase_money'] += $lists_v['purchase_money'];
                                    $status = true;
                                }
                            }
                        }
                        if(!isset($status)){
                            $list[] = $lists_v;
                        }
                    }
                }
                foreach ($list as $list_k=>$list_v){
                    $list_v[$field] = $sup_v['id'];
                    $list_v['customer_name'] = $sup_v['customer_name'];
                    $list_v['customer_type_name'] = $sup_v['type_name'];

                    if(isset($list_v['advance_rate'])){
                        $list_v['basic_nmber'] = round( ($list_v['i_number']/$list_v['advance_rate']) ,$number_decimal).$list_v['more_unit_name'];
                    }
                    if($list_v['i_number']!=0){
                        $list_v['unit_price'] = round( ($list_v['all_money']/$list_v['i_number']) ,$price_decimal);
                        $list_v['tax_included'] = round( ($list_v['money_tax_price']/$list_v['i_number']) ,$price_decimal);
                    }else{
                        $list_v['unit_price'] = 0;
                        $list_v['tax_included'] = 0;
                    }

                    $list_v['i_number'] = round( ($list_v['i_number']) ,$number_decimal);
                    $list_v['all_money'] = round( ($list_v['all_money']) ,$price_decimal);
                    $list_v['tax_money'] = round( ($list_v['tax_money']) ,$price_decimal);
                    $list_v['money_tax_price'] = round( ($list_v['money_tax_price']) ,$price_decimal);

                    $listarr[] = $list_v;
                }
                $sup[$sup_k]['list'] = $list;
            }

            $count = [
                'i_number'=>0,
                'unit_price'=>0,
                'all_money'=>0,
                'tax_included'=>0,
                'tax_money'=>0,
                'money_tax_price'=>0,
            ];
            $counts = $count;
            $counts['s_name'] = '合计';$counts['s_type'] = 3;
            $data = [];
            $now = isset($listarr[0][$field])?$listarr[0][$field]:'';
            foreach ($listarr as $list_k=>$list_v){
                if($list_v[$field]!=$now){
                    $now = $list_v[$field];
                    $row = $count;$row['s_name'] = '小计';$row['s_type'] = 2;
                    $row['unit_price'] = round($row['all_money']/$row['i_number'],$price_decimal);
                    $data[] = $row;
                    foreach ($count as $count_k=>$count_v){
                        $count[$count_k] = $list_v[$count_k];
                        $counts[$count_k] += $list_v[$count_k];
                    }
                }else{
                    foreach ($count as $count_k=>$count_v){
                        $count[$count_k] += $list_v[$count_k];
                        $counts[$count_k] += $list_v[$count_k];
                    }
                }
                $list_v['s_type'] = 1;
                $data[] = $list_v;
                if($list_k==(sizeof($listarr)-1) || sizeof($listarr)==1){
                    $row = $count;$row['s_name'] = '小计';$row['s_type'] = 2;
                    $row['unit_price'] = round($row['all_money']/$row['i_number'],$price_decimal);
                    $row['all_money'] = round($row['all_money'],$price_decimal);
                    $data[] = $row;
                }
            }
            $counts['all_money'] = round($counts['all_money'],$price_decimal);
            $counts['unit_price'] = $counts['i_number']>0?(round($counts['all_money']/$counts['i_number'],$price_decimal)):0;
            $counts['tax_included'] = round($counts['tax_included'],$price_decimal);
            $counts['tax_money'] = round($counts['tax_money'],$price_decimal);
            $counts['money_tax_price'] = round($counts['money_tax_price'],$price_decimal);
            $data[] = $counts;

            $result = array("count" => sizeof($data), "list" => $data);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
